﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class IntegerVariable : Variable
    {
        private int innerDefaultIntegerValue;//Taking into account that "base" is now abstract class
        private int innerCurrentIntegerValue;//and to increase performance, we should avoid use
        //tryParse() in properies

        public int DefaultIntegerValue
        {
            get
            {
                //int retValue;
                //if (!int.TryParse(base.DefaultValue,out retValue))
                //{
                //    throw new ArgumentException();
                //}
                return innerDefaultIntegerValue;
            }
            set
            {
                innerDefaultIntegerValue = value;
                base.DefaultValue = value.ToString();
            }

        }

        public override string CurrentValue
        {
            get { return base.CurrentValue; }
            set
            {
                if (currentValue != value)
                {
                    if (!int.TryParse(value, out innerCurrentIntegerValue))
                    {
                        throw new ArgumentException();
                    }
                    currentValue = value;
                    isChanged = true;
                }

            }
        }

        public int CurrentIntegerValue
        {
            get
            {
                return innerCurrentIntegerValue;
            }
            set
            {
                innerCurrentIntegerValue = value;
                base.CurrentValue = value.ToString();
            }
        }

        public IntegerVariable(string name, string disc, string defValue,int ID)
            : base(name, disc, defValue,ID)
        {
            type = typeof(int);
            if (!int.TryParse(base.DefaultValue, out innerDefaultIntegerValue))
            {
                throw new ArgumentException();
            }
        }



        public static IntegerVariable ConvertFromVariableInstance(Variable var)//for DB deserialyzation
        {
            if ((var.Type).Equals(typeof(int)))
            {
                try
                {

                    //IntegerVariable ret = new IntegerVariable(var.Name, var.Description, var.DefaultValue);
                    IntegerVariable ret = (IntegerVariable)var;

                    int tmp;
                    if (!int.TryParse(var.CurrentValue, out tmp))
                    {
                        return null;
                    }
                    ret.CurrentIntegerValue = tmp;

                    if (!int.TryParse(var.DefaultValue, out tmp))
                    {
                        return null;
                    }
                    ret.DefaultIntegerValue = tmp;

                    return ret;
                }
                catch (Exception e)
                {
                    return null;
                }

            }
            else
            {
                return null;
            }
        }
    }
}
